<?php
namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Money extends Model
{
	/**
	 * 管理收入结算
	 */
	public static function ManagementRevenueSettlement($id){
		DB::beginTransaction();
        $info = DB::table('partner_income')->where('id',$id)->lockForUpdate()->first();
        if($info->status==1){
            DB::rollBack();
            return ['status'=>0,'不能重复结算'];
        }

        $money = $info->money;
        $uid = $info->uid;

        $step1 = DB::table('partner_moneylog')->insert([
            'uid'=>$uid,
            'affect'=>$money,
            'add_time'=>time(),
            'type'=>4,
            'current_money'=>$info->money+$money,
            'desc'=>date('Y年m月d日',$info->add_time).'管理收入结算',
        ]);
        $step2 = DB::table('user_tree')->where('uid',$uid)->update([
            'money'=>DB::raw("`money`+".$money),
            'total_money'=>DB::raw("`total_money`+".$money),
            ]);
        $step3 = DB::table('partner_income')->where('id',$id)->update(['status'=>1,'pay_ymd'=>date('Ymd')]);
        $step4 = DB::table('integral')->where('uid',$uid)->update([
            'money'=>DB::raw("`money`-".$money),
        ]);

        if($step1!==false && $step2!==false && $step3!==false && $step4!==false){
            DB::commit();
            return ['status'=>1];
        }
        DB::rollBack();
        return ['status'=>0,'msg'=>"事物回滚"];
	}
}